<?php
/**
 * 用户管理模型
 *
 */
class Model_User extends Model_base{
	
	// 用户表名
	private $userTable ='';
	private $departmentTable ='';
	
	/**
	 * 构造函数
	 *
	 */
	public function __construct(){
		parent::__construct();
		$this->userTable = tabname('user');
		$this->departmentTable = tabname('department');
	}
	
	/**
	 * 用户登录
	 *
	 * @param string $username :用户名
	 * @param string $password :密码
	 * @return int 0:成功 -1:用户名不对 -2:密码不对 -3:帐号被禁用  -4:用户名或密码为空
	 */	
	public function login($username,$password,$remember=false){
		if(empty($username) || empty($password)){
			return -4;
		}
		$row = $this->db->getRow($this->userTable)->where(array('username'=>$username))->result();
		if($row){
			if($row['is_locked'] == 1){		
				return -3;
			}
			elseif ($row['password'] == md5($password)){
				$sets_arr = array(
	         		'failed_times'=>0,
	         		'last_login_at'=>time(),
	         		'last_login_ip'=>$_SERVER['REMOTE_ADDR'],
	         	);
	         	$this->updateUser($row['id'],$sets_arr);
	         	
				// 写SESSION
				QP_Session_Session::set(array(
					'login_userid'=>$row['id'],
					'login_displayname'=>$row['sirname'].$row['givenname'],
					'login_username'=>$row['username'],
					'login_is_operator'=>$row['is_operator'],
					'login_is_admin'=>$row['is_admin'],
				));
				if($remember){
					QP_Cookie_Cookie::set('login_userid',$row['id'],86400 * 7,$_SERVER[SERVER_NAME],true);
					QP_Cookie_Cookie::set('login_displayname',$row['sirname'].$row['givenname'],86400 * 7,$_SERVER[SERVER_NAME],true);
					QP_Cookie_Cookie::set('login_username',$row['username'],86400 * 7,$_SERVER[SERVER_NAME],true);
					QP_Cookie_Cookie::set('login_is_operator',$row['is_operator'],86400 * 7,$_SERVER[SERVER_NAME],true);
					QP_Cookie_Cookie::set('login_is_admin',$row['is_admin'],86400 * 7,$_SERVER[SERVER_NAME],true);
				}
				return 0;
			}
			else{
				$sets_arr = array(
	         		'failed_times'=>$row['failed_times']+1,
	         		'last_login_at'=>time(),
	         		'last_login_ip'=>$_SERVER['REMOTE_ADDR'],
		         	'is_locked'=>0,
	         	 );
	         	 if($sets_arr['failed_times'] >= 6) {
	         	 	$sets_arr['is_locked'] = 1;
	         	 }
	         	 $this->updateUser($row['id'],$sets_arr);
				 return -2;
			}
		}
		else{
			return -1;
		}
	}
	
	public function auth($id, $password){
		return $this->db->count($this->userTable,array('id'=>$id,'password'=>md5($password)));
	}
	
	/**
	 * 用户登出
	 *
	 * @return boolean
	 */
	public function logout(){
		// 删除SESSION
		QP_Session_Session::remove(array(
			'login_userid',
			'login_displayname',
			'login_username',
			'login_is_operator',
			'login_is_admin',
		));
		// 删除COOKIE
		QP_Cookie_Cookie::remove('login_userid');
		QP_Cookie_Cookie::remove('login_displayname');
		QP_Cookie_Cookie::remove('login_username');
		QP_Cookie_Cookie::remove('login_is_operator');
		QP_Cookie_Cookie::remove('login_is_admin');
		return true;
	}

	public function countUser(){
		return $this->db->count($this->userTable,array('is_deleted'=>0));
	}
	
	public function listUser($start=null,$length=null){
		//$sql = "select * from ".$this->userTable." left join ".$this->departmentTable." as d on dept_id = d.id";
		$sql = "SELECT u.id, u.username, u.dept_id, d.name AS dept, u.sirname, u.givenname, persona_id, tel, mob, is_locked, is_operator, last_login_at, last_login_ip"
				." FROM ".$this->userTable." AS u"
				." LEFT JOIN ".$this->departmentTable." AS d ON u.dept_id = d.id"
				." WHERE u.is_deleted =0";
		if($length > 0){
			$sql .= " LIMIT $start,$length";
		}
		$lists = $this->db->fetchAll($sql);
		foreach ($lists as $key=>$row){
			if(empty($row['last_login_at'])){
				$lists[$key]['last_login_at'] = '';
			}else{
				$lists[$key]['last_login_at'] = date('Y-m-d H:i:s',$row['last_login_at']);
			}
		}
		return $lists;
	}
	
	/**
	 * 添加用户
	 *
	 * @param unknow_type $sets
	 */
	public function addUser($sets){
		// 判断用户信息完整性，由页面easyui-validatebox负责处理
				
		// 判断用户名是否已存在
		if($this->db->count($this->userTable,array('username'=>$sets['username']))){
			return false;
		}
		$this->db->insert($this->userTable,$sets);
		$user_id = $this->db->lastInsertId();
		if($user_id>0){
			return true;
		}
		else{
			return false;
		}
	}
	
	/**
	 * 更新用户
	 * @param int $id
	 * @param unknow_type $sets
	 */
	public function updateUser($id,$sets){
		// 判断用户信息完整性，由页面easyui-validatebox负责处理
		if(empty($id)){
			return false;
		}else{
			$ret = $this->db->update($this->userTable,$sets,array('id'=>$id));
			if($ret){
				return true;
			}
			else{
				return false;
			}
		}
	}
	
	/**
	 * 软删除用户
	 *
	 * @param int $id
	 */
	public function delUser($id){
		if(empty($id)){
			return false;
		}else{
			$ret = $this->db->update($this->userTable,array('is_deleted'=>1),array('id'=>$id));
			if($ret){
				return true;
			}
			else{
				return false;
			}
		}
	}
}